// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/service/module/album_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/album"
	albumModel "gin-sns/app/model/module/album"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var AlbumService = newAlbumService()

func newAlbumService() *albumService {
	return &albumService{}
}

type albumService struct {
}

//根据主键查询数据
func (s *albumService) SelectRecordById(id int64) (*albumModel.Entity, error) {
	return album.AlbumDao.SelectById(id)
}

//
func (s *albumService) SelectRecordsIn(idarr []int64) ([]albumModel.Entity, error) {

	list, err := album.AlbumDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *albumService) DeleteRecordById(id int64) bool {
	return album.AlbumDao.DeleteById(id)
}

//批量删除数据记录
func (s *albumService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := album.AlbumDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *albumService) AddSave(req *albumModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity albumModel.Entity

	//entity.Id = req.Id
	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.CountNum = req.CountNum
	entity.CountPlay = req.CountPlay
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := album.AlbumDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *albumService) EditSave(req *albumModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &albumModel.Entity{Id: req.Id}
	ok, err := album.AlbumDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.CountNum = req.CountNum
	entity.CountPlay = req.CountPlay
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return album.AlbumDao.Update(entity)
}

//根据条件查询数据
func (s *albumService) SelectListAll(params *albumModel.SelectPageReq) ([]albumModel.Entity, error) {
	return album.AlbumDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *albumService) SelectListByPage(params *albumModel.SelectPageReq) ([]albumModel.Entity, *page.Paging, error) {
	return album.AlbumDao.SelectListByPage(params)
}

// 导出excel
func (s *albumService) Export(param *albumModel.SelectPageReq) (string, error) {
	head := []string{"歌单id", "用户id", "歌单名称", "歌单简介", "歌单图片logo", "歌曲数量", "歌单播放数量", "被关注数量", "0未审核 1正常状态 2审核未通过 3删除", "创建时间", "最后更新时间"}
	col := []string{"id", "user_id", "title", "description", "logo", "count_num", "count_play", "count_follow", "status", "create_time", "update_time"}
	return album.AlbumDao.SelectListExport(param, head, col)
}
